'''
Created on Mar 31, 2011

@author: leo
'''

import gcp
import unittest
import json

google_cloud_print = gcp.GoogleCloudPrint(auth_method='GoogleLogin auth', token='DQAAALQAAACwgocUiPZhTEBrcs5a74DXQyu1dB3d6UbbiI0pq8gznouw5UO6cURI6VfX2YRiKhN_EIo-iaYC6pndWytro_Vu1jBKNCMWdZ_qfk7xLbYFyrbfplpc_D0N1MRtxUeMotevrHYVdgXQggYA30zIo4yEChLoOkzRASh2JsEY5L8kxORLzKWZrJo6iXNMv_7wABKqJKCuWTsGlzNx0H8MwmSKBNM1FvCACH8vQft_EXZ3D9ceeRo-YgiP96iybl_fyqc')

class Test(unittest.TestCase):
    

    def testSearch(self):
        data = google_cloud_print.search_printers()
        json_data = json.loads(data)
        self.assertTrue(json_data['success'])
        self.assertEquals(type(json_data['printers']).__name__ , list.__name__)
        pass

    def testPrinter(self):
        data = google_cloud_print.search_printers() # fetch printer list first
        json_data = json.loads(data)
        self.assertTrue(json_data['success'])
        self.assertEquals(type(json_data['printers']).__name__ , list.__name__)
        
        printers = json.loads(data)['printers']
        for printer in printers:
            data = google_cloud_print.get_printer_info(printer['id'])
            json_data = json.loads(data)
            self.assertTrue(json_data['success'])
            self.assertEquals(json_data['printers'][0]['id'], printer['id'])
        pass
    
    def testSubmitTextContent(self):
        data = google_cloud_print.search_printers() # fetch printer list first
        json_data = json.loads(data)
        self.assertTrue(json_data['success'])
        self.assertEquals(type(json_data['printers']).__name__ , list.__name__)
        
        printers = json.loads(data)['printers']
        for printer in printers:
            data = google_cloud_print.submit_print_job(
                                                       printer['id'], 
                                                       contentType='text/plain', 
                                                       content='Text Content'
                                                       )
            print data
            json_data = json.loads(data)
            self.assertTrue(json_data['success'])
        pass

    def testSubmitTextFile(self):
        data = google_cloud_print.search_printers() # fetch printer list first
        json_data = json.loads(data)
        self.assertTrue(json_data['success'])
        self.assertEquals(type(json_data['printers']).__name__ , list.__name__)
        
        printers = json.loads(data)['printers']
        for printer in printers:
            data = google_cloud_print.submit_print_job(
                                                       printer['id'], 
                                                       contentType='text/plain', 
                                                       content=open('text_file.txt', 'r')
                                                       )
            print data
            json_data = json.loads(data)
            self.assertTrue(json_data['success'])
        pass
    
    def testSubmitJpgFile(self):
        data = google_cloud_print.search_printers() # fetch printer list first
        json_data = json.loads(data)
        self.assertTrue(json_data['success'])
        self.assertEquals(type(json_data['printers']).__name__ , list.__name__)
        
        printers = json.loads(data)['printers']
        for printer in printers:
            data = google_cloud_print.submit_print_job(
                                                       printer['id'], 
                                                       contentType='image/jpeg', 
                                                       content=open('jpg_file.JPG', 'rb')
                                                       )
            print data
            json_data = json.loads(data)
            self.assertTrue(json_data['success'])
        pass
    

if __name__ == "__main__":
    unittest.main()